package com.wanjia.bpsapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanjia.bpsapi.exception.ControllerException;
import com.wanjia.bpsapi.exception.PasswordNotMatchException;
import com.wanjia.bpsapi.exception.UserNotFoundException;
import com.wanjia.bpsapi.pojo.dto.UserDto;
import com.wanjia.bpsapi.pojo.dto.UserVo;
import com.wanjia.bpsapi.pojo.entity.User;
import com.wanjia.bpsapi.mapper.UserMapper;
import com.wanjia.bpsapi.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 登陆用户表 服务实现类
 * </p>
 *
 * @author wyn
 * @since 2023-02-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUser(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        String newpassword = DigestUtils.md5DigestAsHex(password.getBytes());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User myuser = userMapper.selectOne(wrapper);
        if(Objects.isNull(myuser)){
            throw new ControllerException("用户不存在");
        }
        String username1 = myuser.getUsername();
        String password1 = myuser.getPassword();
        if (username1 == null) {
            throw new UserNotFoundException("用户名不存在");
        }
        if (!newpassword.equals(password1)){
            throw new PasswordNotMatchException("密码错误");
        }
        return myuser;
    }

    @Override
    public int addUser(User user) {
        String username = user.getUsername();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User myuser = userMapper.selectOne(wrapper);
        if (myuser!=null){
            throw new ControllerException("用户名不可重复");
        }
        String password = user.getPassword();
        String newpassword = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(newpassword);
        int i = userMapper.insert(user);
        if (i!=1){
            throw new ControllerException("新增用户失败");
        }
        return i;
    }

    @Override
    public int updateUser(UserVo user) {
        String username = user.getUsername();
        //获取原密码加密
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("password",password);
        User user1 = userMapper.selectOne(wrapper);
        if (user1==null){
            throw new ControllerException("原密码输入错误");
        }
        //获取新密码加密
        User user2 =new User();
        String newpassword = DigestUtils.md5DigestAsHex(user.getNewPassword().getBytes());
        user2.setId(user.getId());
        user2.setPhone(user.getPhone());
        user2.setStatus(user.getStatus());
        user2.setUsername(username);
        user2.setRole(user.getRole());
        user2.setPassword(newpassword);
        int i = userMapper.updateById(user2);
        if (i!=1){
            throw new ControllerException("更新用户失败");
        }
        return i;
    }

    @Override
    public List<User> getUserList(String username,Integer pageNo, Integer pageSize) {
        List<User> users = userMapper.selectUserList(username,pageNo,pageSize);
        /*if (users.size()==0){
            throw new ControllerException("获取用户失败");
        }*/
        return users;
    }
}
